﻿Public Class oCompany
    Public Function UpdateJsonCompany(ByVal companyCode As String, ByVal companyName As String, ByVal isActive As Boolean, ByVal address As String, _
                                         ByVal contactPerson As String, ByVal contactPhone As String, _
                                        ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim params = New Object() {companyCode, companyName, isActive, address, contactPerson, contactPhone}
                Dim query As New StringBuilder
                If isInsert = True Then
                    query.Append("INSERT INTO [oCompany] ")
                    query.Append(" ([CompanyCode] ")
                    query.Append(" ,[CompanyName] ")
                    query.Append(" ,[IsActive] ")
                    query.Append(" ,[Address] ")
                    query.Append(" ,[ContactPerson] ")
                    query.Append(" ,[ContactPhone]) ")
                    query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6)")
                    Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                Else
                    query.Append("UPDATE  [oCompany] SET ")
                    query.Append(" [CompanyName] = @Param1")
                    query.Append(" ,[IsActive] = @Param2")
                    query.Append(" ,[Address] = @Param3")
                    query.Append(" ,[ContactPerson] = @Param4")
                    query.Append(" ,[ContactPhone] = @Param5")
                    query.Append(" WHERE [CompanyCode] = @Param6")
                    connect.Object_Execute_SAP(query.ToString(), New Object() {companyName, isActive, address, contactPerson, contactPhone, companyCode})
                End If
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function UpdateCompany(ByVal data As DataSet, ByVal databaseName As String, ByVal isInsert As Boolean) As String
        Dim connect As New Connection()
        Try
            For Each row As DataRow In data.Tables("oCompany").Rows
                Dim errMsg As String = connect.setSQLDB(databaseName)
                If errMsg.Length = 0 Then
                    Dim params = New Object() {row("CompanyCode"), row("CompanyName"), row("IsActive"), row("Address"), row("ContactPerson"), row("ContactPhone")}
                    Dim query As New StringBuilder
                    If isInsert = True Then
                        query.Append("INSERT INTO [oCompany] ")
                        query.Append(" ([CompanyCode] ")
                        query.Append(" ,[CompanyName] ")
                        query.Append(" ,[IsActive] ")
                        query.Append(" ,[Address] ")
                        query.Append(" ,[ContactPerson] ")
                        query.Append(" ,[ContactPhone]) ")
                        query.Append(" VALUES (@Param1,@Param2,@Param3,@Param4,@Param5,@Param6)")
                        Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), params)
                    Else
                        query.Append("UPDATE  [oCompany] SET ")
                        query.Append(" [CompanyName] = @Param1")
                        query.Append(" ,[IsActive] = @Param2")
                        query.Append(" ,[Address] = @Param3")
                        query.Append(" ,[ContactPerson] = @Param4")
                        query.Append(" ,[ContactPhone] = @Param5")
                        query.Append(" WHERE [CompanyCode] = @Param6")
                        connect.Object_Execute_SAP(query.ToString(), New Object() {row("CompanyName"), row("IsActive"), row("Address"), row("ContactPerson"), row("ContactPhone"), row("CompanyCode")})
                    End If
                Else
                    Return errMsg
                End If
            Next
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function GetAllCompany(ByVal dataBaseName As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCompany")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCompany")
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function GetCompanyByCode(ByVal dataBaseName As String, ByVal companyCode As String) As DataSet
        Dim connect As New Connection()
        Try
            Dim ds As New DataSet("oCompany")
            Dim errMsg As String = connect.setSQLDB(dataBaseName)
            If errMsg.Length = 0 Then
                ds = connect.ObjectGetAll_Query_SAP("SELECT * FROM oCompany WHERE CompanyCode = @Param1", New Object() {companyCode})
            End If
            Return ds
        Catch ex As Exception
            Throw ex
        End Try
    End Function
    Public Function UpdateJsonCompanyProfile(ByVal companyCode As String, ByVal address As String, _
                                         ByVal contactPerson As String, ByVal contactPhone As String, _
                                        ByVal databaseName As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("UPDATE  [oCompany] SET ")
                query.Append(" [Address] = @Param1")
                query.Append(" ,[ContactPerson] = @Param2")
                query.Append(" ,[ContactPhone] = @Param3")
                query.Append(" WHERE [CompanyCode] = @Param4")
                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {address, contactPerson, contactPhone, companyCode})
                Return count.ToString()
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
    Public Function UpdateJsonCompanySetting(ByVal companyCode As String, ByVal pointExpireType As String, ByVal pointExpireFixedDate As DateTime, _
                                         ByVal pointExpireDays As Double, ByVal pointConflictMethod As String, ByVal pointChangeAction As String, ByVal gradeMethod As String, _
                                        ByVal promotionConflictMethod As String, ByVal databaseName As String) As String
        Dim connect As New Connection()
        Try
            Dim errMsg As String = connect.setSQLDB(databaseName)
            If errMsg.Length = 0 Then
                Dim query As New StringBuilder
                query.Append("UPDATE  [oCompany] SET ")
                query.Append(" [PointExpireType] = @Param1")
                query.Append(" ,[PointExpireFixedDate] = @Param2")
                query.Append(" ,[PointExpireDays] = @Param3")
                query.Append(" ,[PointConflictMethod] = @Param4")
                query.Append(" ,[PointChangeAction] = @Param5")
                query.Append(" ,[GradeMethod] = @Param6")
                query.Append(" ,[PromotionConflictMethod] = @Param7")
                query.Append(" WHERE [CompanyCode] = @Param8")

                Dim count As Integer = connect.Object_Execute_SAP(query.ToString(), New Object() {pointExpireType, pointExpireFixedDate, pointExpireDays, _
                                                                pointConflictMethod, pointChangeAction, gradeMethod, promotionConflictMethod, companyCode})
                Return count.ToString()
            Else
                Return errMsg
            End If
        Catch ex As Exception

            Return ex.Message
        End Try
        Return String.Empty
    End Function
End Class
